package com.yang.controller.data;

import com.yang.domain.Bill;
import com.yang.service.BillService;
import com.yang.utils.ThreadLocalUtil;
import com.yang.vo.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "账单模块")
@Slf4j
@RequestMapping("/bill")
@RestController
public class BillController {

    @Autowired
    private BillService billService;

    @ApiOperation(value = "获取账单列表",
                  notes = "status表示要查看的账单状态，1收入账单，-1支出账单")
    @GetMapping("/{status}/list")
    public ResponseResult getBillList(@PathVariable Integer status){
        log.info("id为{}的用户获取了{}账单列表", ThreadLocalUtil.get(),status==1?"收入":(status==-1?"支出":"错误的"));
        return billService.getBillList(status);
    }

    @ApiOperation("添加账单")
    @PostMapping("/{status}")
    public ResponseResult addBill(@PathVariable Integer status, @RequestBody Bill bill){
        log.info("id为{}的用户添加了{}账单列表", ThreadLocalUtil.get(),status==1?"收入":(status==-1?"支出":"错误的"));
        return billService.addBill(status,bill);
    }

    @ApiOperation("删除帐单")
    @DeleteMapping
    public ResponseResult deleteBill(@RequestParam("id")Long id){
        log.info("id为{}的用户删除了id为{}的账单",ThreadLocalUtil.get(),id);
        return billService.deleteBill(id);
    }


    @ApiOperation("修改账单")
    @PutMapping("/{status}")
    public ResponseResult updateBill(@RequestBody Bill bill,
                                     @PathVariable Integer status){
        log.info("id为{}的用户修改了{}账单:\n{}",ThreadLocalUtil.get(),
                (status==1?"收入":(status==-1?"支出":"错误的")),bill);
        return billService.updateBill(bill,status);
    }

}
